package com.gxs.waqas.localfolders.utils;

import java.io.File;
import java.util.List;

public class FileUtilsForLocalFolders {

	public static void main(String [] args)
	{
		try
		{
			// getCountsOfFilesFromFolders("S:\\Pictures and Videos\\ORGANIZED_CAMERA_PICTURES");
			findNestedFolders("S:\\Pictures and Videos\\ORGANIZED_CAMERA_PICTURES");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	
	public static void findNestedFolders(String location) throws Exception
	{
		File folder = new File(location);

		
		String [] allFolders = folder.list();
		
		for (int i=0; i<allFolders.length; i++)
		{
			File tempFile = new File(folder, allFolders[i]);
			
			if(tempFile.isDirectory())
			{
				if(doesFolderHaveChildFolders(tempFile.getAbsolutePath()))
					System.out.println(tempFile.getName() + " has child folders");
			}
		}
	}
	
	public static boolean doesFolderHaveChildFolders(String directoryName)
	{
		   File directory = new File(directoryName);

		    // get all the files from a directory
		    File[] fList = directory.listFiles();
		    for (File file : fList) 
		    {
		    	if (file.isDirectory())
		    	{
		    		return true;
		    	}
		    }
		
		return false;
	}
	
	public static void getCountsOfFilesFromFolders(String location) throws Exception
	{
		File folder = new File(location);

		
		String [] allFolders = folder.list();
		
		for (int i=0; i<allFolders.length; i++)
		{
			File tempFile = new File(folder, allFolders[i]);
			
			if(tempFile.isDirectory())
			{
				int countOfFiles = tempFile.list().length;
				int countOfItems =  tempFile.listFiles() == null ? 0 : tempFile.listFiles().length;
				System.out.println("\"" + allFolders[i] + "\",\"" + countOfFiles + "\"" );
			}
		}
	}
}
